package com.hoopchina.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.Map.Entry;

import android.net.Uri;

/**
 * Url utility class
 * @author Si Wei
 *
 */
public class UrlUtils {
	public static String             UrlRegister  = "http://www.example.com/account/register.html";
	public static String             UrlUpdate    = "http://10.57.49.53/tuan/update.xml";
	/*
	 * home栏目新闻url
	 */
	public static final String[]     HOME_NEWS_URLS={
		"http://news.hoopchina.com/news.xml",              //焦点新闻
		"http://news.hoopchina.com/news_NBA.xml",            //本地新闻
		"http://news.hoopchina.com/news_CBA.xml",//国内
		"http://news.hoopchina.com/news_WNBA.xml",//国际
		"http://news.hoopchina.com/news_NCAA.xml",      //军事
		"http://news.hoopchina.com/news_zb.xml",//女人
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=enternews",//娱乐
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=sportnews",//体育
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=socianews",//社会	              
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=finannews",//财经
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=autonews",//汽车
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=technnews",//科技
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=housenews",//房产
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=edunews",  //教育
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=gamenews",//游戏
		"http://newsmobile.baidu.com/n?m=app&v=nsatom&class=internet"//互联网
	};
	/*
	 * home栏目分类字符串
	 */
	public static String[] HOME_ITEMS =   { "全部", "NBA", "CBA","WNBA","NCAA","装备",
		"自定义导航" };
	
	public static String[] GOAL_ITEMS =   { "全部", "热门", "英超","西甲","意甲","德甲","中国","雷囧","转会",
	"自定义导航" };
	
	private static final String[]    ServerUrls   = {
		"http://10.57.49.53/tuan/groupbuy.ashx",   // 0
		"http://10.57.49.53/tuan/groupbuy.ashx",
		"http://10.57.49.53/tuan/coupon.ashx",
		"http://10.57.49.53/tuan/groupbuy.ashx",
		"http://10.57.49.53/tuan/cities.ashx",
		null,
		null,
		null,
		null,
		null,
		"http://10.57.49.53/tuan/account.ashx",   // 10
		"http://10.57.49.53/tuan/account.ashx",
		"http://10.57.49.53/tuan/user.ashx",
		"http://10.57.49.53/tuan/user.ashx",
		null,
		null,
		null,
		null,
		null,
		null,
		"http://10.57.49.53/tuan/buy.ashx",       // 20
		"http://10.57.49.53/tuan/buy.ashx",
		"http://10.57.49.53/tuan/buy.ashx"
	};
	
	private static String            mCharAnd      = "&";
	private static String           mCharEqual    = "=";
	private static String            mCharQuestion = "?";
	
	/**
	 * Solve the mapping to URL
	 * @param aParameters key/value pairs
	 * @return destination URL
	 */
	public static String getUrlByParameters( int aRequestType, Map<String, String> aParameters ) {
		String retString = ServerUrls[ aRequestType ];
		
		if ( aParameters.size() > 0 ) {
			StringBuilder sb = new StringBuilder( retString );
			// => "/xxx?"
			sb.append( mCharQuestion );
			// appending parameters => "/xxx?a=1&b=2&"
			for( Iterator<Entry<String, String>> iter = aParameters.entrySet().iterator(); iter.hasNext(); ) {
				Entry<String, String> entry = iter.next();
				try {
					sb.append( entry.getKey() )
					  .append( mCharEqual )
					  .append( URLEncoder.encode( entry.getValue(), "UTF-8" ) )
					  .append( mCharAnd );
				} catch ( UnsupportedEncodingException e ) {
					return null;
				}
			}
			// trim last "&" at right
			retString = retString.substring( 0, retString.length() - 1 );
		}
		
		return retString;
	}
	
	/**
	 * Simple wrapper of URI converter
	 * @param aUrl URL string
	 * @return URI object
	 */
	public static Uri getUriFromUrlString( String aUrl ) {
		return Uri.parse( aUrl );
	}
	
    public static String getNameFromUrl( String aUrlString ) {
        if ( null == aUrlString || aUrlString.equalsIgnoreCase( "" ) )
            return UUID.randomUUID().toString();
        return UUID.nameUUIDFromBytes( aUrlString.getBytes() ).toString();
    }
    
    public static File getFileFromUrl(String aSavePath,String aUrlString){
        return new File( aSavePath,getNameFromUrl( aUrlString ) );
    }
    
    public static InputStream getInputSreamFromUrl( String urlstr ) throws IOException {
        URL url = new URL( urlstr );
        HttpURLConnection urlconn = ( HttpURLConnection ) url.openConnection();
        InputStream is = urlconn.getInputStream();
        return is;
    }
    public static InputStream getInputStreamFromFile(File aFile) throws IOException
    {
        return new BufferedInputStream(new FileInputStream(aFile));
    }

}
